package com.wyvoid.forum.web.action.forum;

import com.wyvoid.forum.web.action.BaseAction;
import com.wyvoid.forum.domain.Topic;
import com.wyvoid.forum.domain.Post;
import com.wyvoid.forum.service.ITopicService;
import com.wyvoid.forum.service.IPostService;
import com.wyvoid.core.persistent.Pagination;
import com.wyvoid.core.persistent.hibernate.HibernatePage;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

import java.util.List;

/**
 * <p>51void.com</p>
 * User:cjp
 * Date: 2007-5-26
 * Time: 18:32:14
 */
@UrlBinding(value = "/thread.do")
public class TopicAction extends BaseAction {
    private Topic topic;
    @SpringBean
    private ITopicService topicService;
    @SpringBean
    private IPostService postService;
    HibernatePage<Post> posts;
    private long tid;
    private int curPage;

    public void setTopic(Topic topic) {
        this.topic = topic;
    }

    public void setTopicService(ITopicService topicService) {
        this.topicService = topicService;
    }

    public void setPostService(IPostService postService) {
        this.postService = postService;
    }

    public void setTid(long tid) {
        this.tid = tid;
    }

    public Topic getTopic() {
        return topic;
    }


    public HibernatePage<Post> getPosts() {
        return posts;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    @DefaultHandler
    public Resolution listPosts() {
        if (curPage == 0) {
            curPage = 1;
        }

        topic = topicService.getTopic(tid);
        topic.setViewCount(topic.getViewCount() + 1);
        topicService.updateTopic(topic);
        Pagination pagination = new Pagination();
        pagination.setPageNumber(curPage);
        pagination.setPageSize(20);
        posts = postService.getPosts(tid, pagination);
        return getBBSResolution("viewThread");
    }

    public Resolution nextTopic() {
        if (curPage == 0) {
            curPage = 1;
        }


        topic = topicService.getNextTopic(tid);
        if (topic != null) {
            topic.setViewCount(topic.getViewCount() + 1);
            topicService.updateTopic(topic);
            Pagination pagination = new Pagination();
            pagination.setPageNumber(curPage);
            pagination.setPageSize(20);
            posts = postService.getPosts(topic.getId(), pagination);
        }
        return getBBSResolution("viewThread");
    }

    public Resolution lastTopic() {
        if (curPage == 0) {
            curPage = 1;
        }

        topic = topicService.getLastTopic(tid);
        if (topic != null) {
            topic.setViewCount(topic.getViewCount() + 1);
            topicService.updateTopic(topic);
            Pagination pagination = new Pagination();
            pagination.setPageNumber(curPage);
            pagination.setPageSize(20);
            posts = postService.getPosts(topic.getId(), pagination);
        }
        return getBBSResolution("viewThread");
    }
}
